﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Osobe;
using SkolskaUstanova;
using MolbeIPotvrde;

namespace ElektronskiDnevnik
{
    public partial class INastavnik : Form
    {

        MySqlDataReader rdr = null;
        MySqlConnection conn = new MySqlConnection("SERVER=etf.ba;" + "DATABASE=ooadtim8;" + "UID=ooadtim8;" + "PASSWORD=ooadtim8;");
        Prijavljivanje stara = new Prijavljivanje();
        String JMBG;
        int odabraniPredmet, odabraniRazred;
        Nastavnik nastavnik;
        public INastavnik(Prijavljivanje f, String maticni)
        {
            InitializeComponent();
            stara = f;
            JMBG = maticni;
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM Nastavnik WHERE JMBG = '" + JMBG + "';", conn);
            try
            {
                conn.Open();
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    nastavnik = new Nastavnik(Convert.ToString(rdr["Ime"]), Convert.ToString(rdr["Prezime"]), Convert.ToString(rdr["JMBG"]), Convert.ToDateTime(rdr["DatumRodjenja"]), Convert.ToString(rdr["MjestoRodjenja"]), Convert.ToString(rdr["Ulica"]) + ' ' + Convert.ToString(rdr["Broj"]), Convert.ToString(rdr["Password"]), Convert.ToInt32(rdr["Kvalifikacija"]), Convert.ToDateTime(rdr["DatumZaposlenja"]));
                    pictureBox1.ImageLocation = Convert.ToString(rdr["URLSlike"]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();
            }
        }

        private void Form4_Paint(object sender, PaintEventArgs e)
        {
            Font font = new Font("Segoe Print", 28, FontStyle.Regular);
            SolidBrush brush = new SolidBrush(Color.Salmon);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString("Dobrodošli, " + nastavnik.Ime, font, brush, 180, 30);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            flowLayoutPanel2.Visible = false;
            flowLayoutPanel1.Visible = true;
            panel1.Visible = false;
            button10.Visible = true;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            flowLayoutPanel2.Visible = true;
            flowLayoutPanel1.Visible = false;
            panel1.Visible = false;
            button10.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            EvidencijaPrisustva nova = new EvidencijaPrisustva(odabraniPredmet, odabraniRazred, conn);
            nova.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            UnosOcjene nova = new UnosOcjene(JMBG, odabraniPredmet, odabraniRazred, conn);
            nova.Show();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OdabirUcenika nova = new OdabirUcenika(conn, odabraniPredmet, odabraniRazred, JMBG, 1);
            nova.Show();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            PromjenaPassworda nova = new PromjenaPassworda(nastavnik.Jmbg, conn, "3");
            nova.Show();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            var rez = MessageBox.Show("Sigurni ste da se zelite odjaviti?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rez == DialogResult.No) return;
            else
            {
                this.Close();
                stara.Show();
            }
        }


        private void Form4_Load(object sender, EventArgs e)
        {
            List<int>np = new List<int>();
            flowLayoutPanel1.Visible = false; ;
            panel1.Visible = false;
            flowLayoutPanel2.Visible = true;
            textBox1.Text = nastavnik.Ime;
            textBox2.Text = nastavnik.Prezime;
            textBox3.Text = nastavnik.Jmbg;
            textBox4.Text = Convert.ToString((nastavnik.DatumRodjenja).Day + "." + (nastavnik.DatumRodjenja).Month + "." + (nastavnik.DatumRodjenja).Year);
            textBox5.Text = nastavnik.MjestoRodjenja;
            textBox6.Text = nastavnik.Adresa;
            textBox7.Text = Convert.ToString(nastavnik.Kvalifikacija);
            textBox8.Text = Convert.ToString((nastavnik.DatumZaposlenja).Day + "." + (nastavnik.DatumZaposlenja).Month + "." + (nastavnik.DatumZaposlenja).Year);
            textBox9.Text = Convert.ToString(nastavnik.RadniStaz) + " godina";
             MySqlDataReader reader = null;
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM NastavnikPredmet WHERE Nastavnik = '" + JMBG + "';", conn);
            try
            {
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                     int p = Convert.ToInt32(reader["Predmet"]);
                     np.Add(p);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
            for (int i = 0; i < np.Count(); i++)
            {
                MySqlDataReader rdr = null;
                MySqlCommand cmd1 = new MySqlCommand("SELECT * FROM Predmet WHERE ID = " + np[i] + ";", conn);
                try
                {
                    rdr = cmd1.ExecuteReader();
                    while (rdr.Read())
                    {
                        Button b = new Button();
                        b.Text = Convert.ToString(rdr["Naziv"]);
                        b.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
                        b.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
                        b.ForeColor = System.Drawing.Color.Salmon;
                        flowLayoutPanel2.Controls.Add(b);
                        b.Size = new System.Drawing.Size(125, 30);
                        b.TabIndex = i + 1;
                        b.Click += new System.EventHandler(this.b_Click);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    if (rdr != null)
                        rdr.Close();
                }
                int razred = 0;
                MySqlCommand cmd2 = new MySqlCommand("SELECT * FROM Razred WHERE Razrednik = '" + nastavnik.Jmbg + "';", conn);
                try
                {
                    reader = cmd2.ExecuteReader();
                    while (reader.Read())
                        razred = Convert.ToInt32(reader["ID"]);
                    if (razred == 0)
                        button8.Enabled = false;
                    else
                        button8.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    if (reader != null)
                        reader.Close();
                }
            }
        }

        private void Form4_FormClosing(object sender, FormClosingEventArgs e)
        {
            stara.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            PregledUspjehaN nova = new PregledUspjehaN(JMBG, odabraniPredmet, odabraniRazred, conn);
            nova.Show();
        }

       private void button11_Click(object sender, EventArgs e)
        {
            OdabirUcenika nova = new OdabirUcenika(conn, odabraniPredmet, odabraniRazred, JMBG, 2);
            nova.Show();
        }

         private void b_Click(object sender, EventArgs e)
          {
            flowLayoutPanel2.Visible = false;
            flowLayoutPanel1.Visible = true;
            button10.Visible = true;
            panel1.Visible = false;
            flowLayoutPanel1.Controls.Clear();
            flowLayoutPanel1.Controls.Add(label8);
            Button b = new Button();
            b = (Button)sender;
            MySqlCommand cmd1 = new MySqlCommand("SELECT * FROM Predmet WHERE Naziv = '" + b.Text + "';", conn);
            try
            {
                rdr = cmd1.ExecuteReader();
                while (rdr.Read())
                {
                    odabraniPredmet = Convert.ToInt32(rdr["ID"]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();
            }
           
            MySqlDataReader reader = null;
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM NastavnikPredRaz WHERE IDNastavnik = '" + JMBG + "' AND IDPredmet = " + odabraniPredmet + ";", conn);
            try
            {
                int j = 0;
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Button b1 = new Button();
                    b1.Text = Convert.ToString(reader["Godina"]) + "-" + Convert.ToString(reader["Odjeljenje"]);
                    b1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
                    b1.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
                    b1.ForeColor = System.Drawing.Color.Salmon;
                    flowLayoutPanel1.Controls.Add(b1);
                    b1.Size = new System.Drawing.Size(125, 30);
                    b1.TabIndex = j + 1;
                    b1.Click += new System.EventHandler(this.b1_Click);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }

         private void b1_Click(object sender, EventArgs e)
         {
             button10.Visible = false;
             flowLayoutPanel1.Visible = false;
             panel1.Visible = true;
             String select = ((Button)sender).Text;
             int god = Convert.ToInt32(select.Substring(0,1));
             String odj = select.Substring(2,1);
             MySqlDataReader reader = null;
             MySqlCommand cmd = new MySqlCommand("SELECT * FROM Razred WHERE Godina = " + god + " AND Odjeljenje = '" + odj + "';", conn);
             try
             {
                 reader = cmd.ExecuteReader();
                 while (reader.Read())
                     odabraniRazred = Convert.ToInt32(reader["ID"]);
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
             finally
             {
                 if (reader != null)
                     reader.Close();
             }
         }

         private void button8_Click(object sender, EventArgs e)
         {
             PravdanjeIzostanakaN nova = new PravdanjeIzostanakaN(JMBG, conn);
             nova.Show();
         }
    }
}
